package com.yc.pay.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * mq配置
 */
@Configuration
public class MqConfig {

    public static final String BUSINESS_EXCHANGE_NAME = "biz.exchange";

    public static final String BUSINESS_QUEUEA_NAME  = "biz.queue";

    public static final String DEAD_LETTER_QUEUE_ROUTING_KEY = "dead.letter.routing.key";

    public static final String DEAD_LETTER_EXCHANGE = "dead.letter.exchange";

    public static final String DEAD_LETTER_QUEUE_NAME = "dead.letter.queue";


    // 声明业务Exchange
    @Bean("bizExchange")
    public FanoutExchange bizExchange(){
        return new FanoutExchange(BUSINESS_EXCHANGE_NAME);
    }

    // 声明死信Exchange
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    @Bean("businessQueue")
    public Queue businessQueue(){
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);

//        args.put("x-message-ttl", 300000); //设置队列的延时属性


//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_ROUTING_KEY);
        return QueueBuilder.durable(BUSINESS_QUEUEA_NAME).withArguments(args).build();
    }

    // 声明死信队列A
    @Bean("deadLetterQueue")
    public Queue deadLetterQueueA(){
        return new Queue(DEAD_LETTER_QUEUE_NAME);
    }


    // 声明业务队列绑定关系
    // 声明业务队列A绑定关系
    @Bean
    public Binding businessBindingA(@Qualifier("businessQueue") Queue queue,
                                    @Qualifier("bizExchange") FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }



    // 声明死信队列B绑定关系
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueue") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_ROUTING_KEY);
    }



}
